package com.indris.sample.slice;

import com.indris.sample.ResourceTable;
import com.indris.material.RippleView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice {

    // 两次点击按钮之间的点击间隔不能少于1500毫秒
    private static final int MIN_CLICK_DELAY_TIME = 1500;
    private static long mLastClickTime;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_activity_ripple_view);
        RippleView rippleView = (RippleView) findComponentById(ResourceTable.Id_btn);
        rippleView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                boolean flag = false;
                long curClickTime = System.currentTimeMillis();
                if ((curClickTime - mLastClickTime) >= MIN_CLICK_DELAY_TIME) {
                    flag = true;
                }
                mLastClickTime = curClickTime;
                if (flag) {
                    final ToastDialog toastDialog = new ToastDialog(getContext())
                            .setText("Ripples Yo! :D")
                            .setAlignment(LayoutAlignment.CENTER | LayoutAlignment.BOTTOM);
                    toastDialog.setDuration(1000);
                    toastDialog.show();
                    toastDialog.setDestroyedListener(new CommonDialog.DestroyedListener() {
                        @Override
                        public void onDestroy() {
                            if (toastDialog != null) {
                                toastDialog.cancel();
                            }
                        }
                    });
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
